#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <erpc_client_setup.h>
#include <erpc_port.h>
#include "c_matrix_client.h"

#define USE_TCP_TRANSPORT

int main(int argc, char *argv[])
{
#ifdef USE_TCP_TRANSPORT
    const char *device = "127.0.0.1";

    if (argc == 2) {
        device = argv[1];
    }
    erpc_transport_t transport = erpc_transport_tcp_init(device, 5555, false);
#else
    const char *device = "COM12";

    if (argc == 2) {
        device = argv[1];
    }
    erpc_transport_t transport = erpc_transport_serial_init(device, 115200);
#endif
    if (transport == NULL) {
        printf("erpc transport init error\n");
        return 0;
    }
    erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
    erpc_client_t client = erpc_client_init(transport, message_buffer_factory);
    if (client == NULL) {
        printf("erpc client init error\n");
        return 0;
    }
    initMatrixServer_client(client);

    Matrix matrix1, matrix2, result = {{0}};
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            matrix1[i][j] = j+1;
            matrix2[i][j] = j+1;
        }
    }
printf("===== Matrix #1 =====\n");
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            printf("%08d  ", matrix1[i][j]);
        }
        printf(";\n");
    }
printf("===== Matrix #2 =====\n");
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            printf("%08d  ", matrix2[i][j]);
        }
        printf(";\n");
    }

    // Call remote function matrixMultiply
    matrixMultiply(matrix1, matrix2, result);
printf("===== Result Matrix =====\n");
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            printf("%08d  ", result[i][j]);
        }
        printf(";\n");
    }

    deinitMatrixServer_client();

#ifdef USE_TCP_TRANSPORT
    erpc_transport_tcp_close(transport);
#else
#endif

    return 0;
}
